package cn.fl.dk.web.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.dk.facade.condition.DkRuleInfoQC;
import cn.fl.dk.facade.constant.DkRuleInfoConstant;
import cn.fl.dk.facade.dto.DkRuleInfoDTO;
import cn.fl.dk.web.service.IDkRuleInfoService;
import cn.fl.dk.web.transform.DkRuleInfoVOTransform;
import cn.fl.dk.web.vo.DkRuleInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.fncprjbscinfo.service.IFncPrjBscInfoService;
import cn.fl.ref.fncprjbscinfo.vo.FncPrjBscInfoVO;
import cn.fl.ref.fribaseinfo.service.IFriBaseInfoService;
import cn.fl.ref.fribaseinfo.vo.FriBaseInfoVO;
import cn.fl.risknew.service.IRiskOverdueService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.ref.fribaseinfo.condition.BankAccInfoQueryCondition;
import fl.ref.fribaseinfo.condition.FriBaseInfoQueryCondition;
import fl.ref.fribaseinfo.dto.BankAccInfoDTO;
import fl.ref.fribaseinfo.service.IBankAccInfoApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Description: 垫款规则 控制层
 */
@Controller
@RequestMapping("/dk/dkruleinfo")
public class DkRuleInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(DkRuleInfoController.class);
	@Autowired
	private IDkRuleInfoService dkRuleInfoService;
	@Autowired
	private IFriBaseInfoService  friBaseInfoService;
	@Autowired
	private IBankAccInfoApiService  bankAccInfoApiService;
	@Autowired
	private IFncPrjBscInfoService fncPrjBscInfoService;

	@Autowired
	private IRiskOverdueService overdueService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		ModelAndView view = new ModelAndView("dk/dkRuleInfo/dkRuleInfoList");
		List<FriBaseInfoVO>  friList = null;
		try {
			FriBaseInfoQueryCondition friqc = new FriBaseInfoQueryCondition();
			friList=friBaseInfoService.searchList(friqc);
		} catch (Exception e) {
			BaseLogUtils.error(logger, "toListPage", "异常"+e.getMessage(), e);
		}
		//查询第三方机构
		List<FriBaseInfoVO> list = this.overdueService.getFriBaseInfoList();
		view.addObject("friBaseList", list);

		view.addObject("frilist",JSON.toJSONString(friList));
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		//规则名称
		String ruleNm = request.getParameter("ruleNm");
		//再融资项目名称
		String refPrjNm = request.getParameter("refPrjNm");
		
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		
		DkRuleInfoQC qc = new DkRuleInfoQC();
		qc.setIsDel(0);
		if (StringUtils.isNotEmpty(ruleNm)) {
			qc.setRuleNm(ruleNm);
		}
		if (StringUtils.isNotEmpty(refPrjNm)) {
			qc.setRefPrjNm(refPrjNm);
		}
		page.setCondition(qc);

		Pager pager = this.dkRuleInfoService.searchListPage(page);
		List<DkRuleInfoDTO> list = (List<DkRuleInfoDTO>) pager.getResultList();
		ListPageVO<DkRuleInfoVO> listPage = new ListPageVO<DkRuleInfoVO>(DkRuleInfoVOTransform.toVOList(list), pager.getTotalCount());
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	
	/**
	 * 新增
	 * @Title: add
	 * @param @param request
	 * @param @param response
	 * @param @return
	 * @param @throws BaseException
	 * @author gaoyongfe
	 * @date 2018-6-29 上午10:10:34
	 * @return Object 
	 * @throws
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "add", "新增对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

		//规则名称
		String ruleNm = request.getParameter("ruleNm");
		//再融资项目id
		String refPrjId = request.getParameter("refPrjId");
		//再融资项目名称
		String refPrjNm = request.getParameter("refPrjNm");
		//收款账户
		String accTypCd = request.getParameter("accTypCd");

		String bkNm = request.getParameter("bkNm");
		String bkNo = request.getParameter("bkNo");
		String rcptUnt = request.getParameter("rcptUnt");
		String accountTypCd = request.getParameter("accountTypCd");
		//最高垫款次数（累计）
		String totalMaxQty = request.getParameter("totalMaxQty");
		//无限制
		String totalMaxQtyCh = request.getParameter("totalMaxQtyCh");
		//最高垫款次数（单资产）
		String singleMaxQty = request.getParameter("singleMaxQty");
		//无限制
		String singleMaxQtyCh = request.getParameter("singleMaxQtyCh");
		//赎回执行
		String ransomExecCd = request.getParameter("ransomExecCd");

		//垫款类型
		String dkStyleCd = request.getParameter("dkStyleCd");

		//是否资管业务
		String isAssBiz = request.getParameter("isAssBiz");
		//第三方机构
		String thdOrgCd = request.getParameter("thdOrgCd");
		// 起租日起始
		String lsDtStart = request.getParameter("lsDtStart");
		String lsDtEnd = request.getParameter("lsDtEnd");

		//第三方机构id
		String thdOrgId = null;

		if(StringUtils.isNotEmpty(thdOrgCd)) {
			String[] s = thdOrgCd.replaceAll("'","").split("_");
			if(s != null && s.length > 0) {
				thdOrgCd = s[0];
				thdOrgId = s[1];
			}
		}


		//起垫逾期天数（含）
		String startOvedayDays = request.getParameter("startOvedayDays");
		//最高逾期天数（含）
		String endOvedayDays = request.getParameter("endOvedayDays");
		
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		DkRuleInfoQC ruleqc= new DkRuleInfoQC();
		if(StringUtils.isNotEmpty(ruleNm)){
			ruleqc.setRuleNm(ruleNm.trim());
			List<DkRuleInfoVO> ruleList = this.dkRuleInfoService.selectList(ruleqc);
			if(CollectionUtils.isNotEmpty(ruleList) && ruleList.size() > 0){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("规则名称重复!");
				return result;
			}
		}

		if(StringUtils.isNotEmpty(dkStyleCd) && "0".equals(dkStyleCd)) {
			if(StringUtils.isEmpty(refPrjId)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("请选择项目!");
				return result;
			}
            // 需要 维护人员手动吧结束的规则 作废后才能继续再次添加规则
            DkRuleInfoQC qc = new DkRuleInfoQC();
            qc.setRefPrjId(new Long(refPrjId));
            List<DkRuleInfoVO> voList = this.dkRuleInfoService.selectList(qc);
            if(voList !=null && CollectionUtils.isNotEmpty(voList)){
                if(DkRuleInfoConstant.DK_RULE_RULESTSCD_1.equals(voList.get(0).getRuleStsCd())){
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("该项目已存在垫款规则请先作废再进行添加");
                    return result;
                }
            }
        }



		DkRuleInfoVO entity = null;
		try {
			entity = new DkRuleInfoVO();
			if(!StringUtils.isEmpty(ruleNm)){
				entity.setRuleNm(ruleNm);
			}
			if(!StringUtils.isEmpty(refPrjId)){
				entity.setRefPrjId(new Long(refPrjId));
			}
			if(!StringUtils.isEmpty(refPrjNm)){
				entity.setRefPrjNm(refPrjNm);
			}
			
			if(!StringUtils.isEmpty(accTypCd)){
				entity.setAccTypCd(accTypCd);
				if(accTypCd.equals(DkRuleInfoConstant.DK_RULE_ACCTYPCD_WS)){
					String accId2 = request.getParameter("accId-2");
					if(!StringUtils.isEmpty(accId2)){
						entity.setAccId(new Long(accId2));
						String accNum = request.getParameter("accNum-2");
						entity.setAccNum(accNum);
					}
					entity.setBkAccTypCd(DkRuleInfoConstant.DK_RULE_ACC_TYPE_1);
				}else if(accTypCd.equals(DkRuleInfoConstant.DK_RULE_ACCTYPCD_JG)){
					String accId3 = request.getParameter("accId-3");
					if(!StringUtils.isEmpty(accId3)){
						entity.setAccId(new Long(accId3));
						String accNum = request.getParameter("accNum-3");
						entity.setAccNum(accNum);
					}
					
					if(!StringUtils.isEmpty(accountTypCd)){
						if(DkRuleInfoConstant.DICT_ACC_TYPE_0.equals(accountTypCd)){// 对公
							entity.setBkAccTypCd(DkRuleInfoConstant.DK_RULE_ACC_TYPE_1);
						}else if(DkRuleInfoConstant.DICT_ACC_TYPE_1.equals(accountTypCd)){// 对私
							entity.setBkAccTypCd(DkRuleInfoConstant.DK_RULE_ACC_TYPE_2);
						}
					}
				}
			}
			
			if(!StringUtils.isEmpty(bkNm)){
				entity.setPayeeBkNm(bkNm);
			}
			if(!StringUtils.isEmpty(bkNo)){
				entity.setPayeeBkNo(bkNo);
			}
			if(!StringUtils.isEmpty(rcptUnt)){
				entity.setRcptUnt(rcptUnt);
			}
			
			if(!StringUtils.isEmpty(totalMaxQtyCh)){
				entity.setTotalMaxQty(DkRuleInfoConstant.DK_RULE_TOTALMAXQTY);
			}else{
				if(!StringUtils.isEmpty(totalMaxQty)){
					entity.setTotalMaxQty(new Integer(totalMaxQty));
				}
			}
			
			if(!StringUtils.isEmpty(singleMaxQtyCh)){
				entity.setSingleMaxQty(DkRuleInfoConstant.DK_RULE_SINGLEMAXQTY);
			}else{
				if(!StringUtils.isEmpty(singleMaxQty)){
					entity.setSingleMaxQty(new Integer(singleMaxQty));
				}	
			}
			
			if(!StringUtils.isEmpty(ransomExecCd)){
				entity.setRansomExecCd(ransomExecCd);
			}


			//垫款类型
			if(!StringUtils.isEmpty(dkStyleCd)){
				entity.setDkStyleCd(dkStyleCd);
			}
			//是否资管业务
			if(!StringUtils.isEmpty(isAssBiz)){
				entity.setIsAssBiz(isAssBiz);
				//第三方机构
				if(!StringUtils.isEmpty(thdOrgCd)){
					entity.setThdOrgCd(thdOrgCd);
				}
                //第三方机构id
                if(!StringUtils.isEmpty(thdOrgId)){
                    entity.setThdOrgId(new Long(thdOrgId));
				}
			}



			if(!StringUtils.isEmpty(startOvedayDays)){
				entity.setStartOvedayDays(new Long(startOvedayDays));
			}
			if(!StringUtils.isEmpty(endOvedayDays)){
				entity.setEndOvedayDays(new Long(endOvedayDays));
			}
			entity.setIsDel(0);
			entity.setRuleStsCd(DkRuleInfoConstant.DK_RULE_RULESTSCD_1);
			entity.setBillStsCd(DkRuleInfoConstant.DK_RULE_BILLSTSCD_2);
			entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			entity.setCrtUsrNm(CurrentThreadContext.getCurrentUserName());
			entity.setRuleNo("DKSN");
			if(StringUtils.isNotEmpty(lsDtStart)){
				entity.setLsDtStart(lsDtStart);
			}
			if(StringUtils.isNotEmpty(lsDtEnd)){
				entity.setLsDtEnd(lsDtEnd);
			}

			boolean idFlue = this.dkRuleInfoService.add(entity);
			if(idFlue){
				result.setSuccess(BaseConstant.IS_YESNO_YES);
				result.setInfo("新增成功!");
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增对象失败,name["+ruleNm+"].", CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "add", "新增对象失败,name["+ruleNm+"].", CurrentThreadContext.getCurrentUserName(), ex);
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "add", "新增对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("数据不存在!");
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		DkRuleInfoVO entity = null;
		String ruleNm = request.getParameter("ruleNm");
		String refPrjId = request.getParameter("refPrjId");
		String refPrjNm = request.getParameter("refPrjNm");
		String accTypCd = request.getParameter("accTypCd");
		String bkNm = request.getParameter("bkNm");
		String bkNo = request.getParameter("bkNo");
		String rcptUnt = request.getParameter("rcptUnt");
		String accountTypCd = request.getParameter("accountTypCd");
		String totalMaxQty = request.getParameter("totalMaxQty");
		String totalMaxQtyCh = request.getParameter("totalMaxQtyCh");
		String singleMaxQty = request.getParameter("singleMaxQty");
		String singleMaxQtyCh = request.getParameter("singleMaxQtyCh");
		String ransomExecCd = request.getParameter("ransomExecCd");
		String lsDtStart = request.getParameter("lsDtStart");
		String lsDtEnd = request.getParameter("lsDtEnd");

		//垫款类型
		String dkStyleCd = request.getParameter("dkStyleCd");
		//是否资管业务
		String isAssBiz = request.getParameter("isAssBiz");
		//第三方机构
		String thdOrgCd = request.getParameter("thdOrgCd");
		//第三方机构id
		String thdOrgId = null;

		if(StringUtils.isNotEmpty(thdOrgCd)) {
			String[] s = thdOrgCd.replaceAll("'","").split("_");
			if(s != null && s.length > 0) {
				thdOrgCd = s[0];
				thdOrgId = s[1];
			}
		}

		String startOvedayDays = request.getParameter("startOvedayDays");
		String endOvedayDays = request.getParameter("endOvedayDays");
		
		DkRuleInfoVO ruleInfoVO = this.dkRuleInfoService.selectById(Long.valueOf(id));
		if(ruleInfoVO == null){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("数据不存在!");
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		// 名称变动才去验证是否重名 
		if(StringUtils.isNotEmpty(ruleNm)){
			if(!ruleNm.equals(ruleInfoVO.getRuleNm())){
				DkRuleInfoQC ruleqc= new DkRuleInfoQC();
				ruleqc.setRuleNm(ruleNm.trim());
				List<DkRuleInfoVO> ruleList = this.dkRuleInfoService.selectList(ruleqc);
				if(CollectionUtils.isNotEmpty(ruleList) && ruleList.size() > 0){
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("规则名称重复!");
					return result;
				}
			}
		}
		
		try {
			entity = new DkRuleInfoVO();
			entity.setId(Long.valueOf(id));
			if(!StringUtils.isEmpty(ruleNm)){
				entity.setRuleNm(ruleNm);
			}
			if(!StringUtils.isEmpty(refPrjId)){
				entity.setRefPrjId(new Long(refPrjId));
			}
			if(!StringUtils.isEmpty(refPrjNm)){
				entity.setRefPrjNm(refPrjNm);
			}
			
			if(!StringUtils.isEmpty(accTypCd)){
				entity.setAccTypCd(accTypCd);
				if(accTypCd.equals(DkRuleInfoConstant.DK_RULE_ACCTYPCD_WS)){
					String accId2 = request.getParameter("accId-2");
					if(!StringUtils.isEmpty(accId2)){
						entity.setAccId(new Long(accId2));
						String accNum = request.getParameter("accNum-2");
						entity.setAccNum(accNum);
					}
					entity.setBkAccTypCd(DkRuleInfoConstant.DK_RULE_ACC_TYPE_1);
				}else if(accTypCd.equals(DkRuleInfoConstant.DK_RULE_ACCTYPCD_JG)){
					String accId3 = request.getParameter("accId-3");
					if(!StringUtils.isEmpty(accId3)){
						entity.setAccId(new Long(accId3));
						String accNum = request.getParameter("accNum-3");
						entity.setAccNum(accNum);
					}
					if(!StringUtils.isEmpty(accountTypCd)){
						if(DkRuleInfoConstant.DICT_ACC_TYPE_0.equals(accountTypCd)){// 对公
							entity.setBkAccTypCd(DkRuleInfoConstant.DK_RULE_ACC_TYPE_1);
						}else if(DkRuleInfoConstant.DICT_ACC_TYPE_1.equals(accountTypCd)){// 对私
							entity.setBkAccTypCd(DkRuleInfoConstant.DK_RULE_ACC_TYPE_2);
						}
					}
				}
			}
			
			if(!StringUtils.isEmpty(bkNm)){
				entity.setPayeeBkNm(bkNm);
			}
			if(!StringUtils.isEmpty(bkNo)){
				entity.setPayeeBkNo(bkNo);
			}
			if(!StringUtils.isEmpty(rcptUnt)){
				entity.setRcptUnt(rcptUnt);
			}
			
			if(!StringUtils.isEmpty(totalMaxQtyCh)){
				entity.setTotalMaxQty(DkRuleInfoConstant.DK_RULE_TOTALMAXQTY);
			}else{
				if(!StringUtils.isEmpty(totalMaxQty)){
					entity.setTotalMaxQty(new Integer(totalMaxQty));
				}
			}
			
			if(!StringUtils.isEmpty(singleMaxQtyCh)){
				entity.setSingleMaxQty(DkRuleInfoConstant.DK_RULE_SINGLEMAXQTY);
			}else{
				if(!StringUtils.isEmpty(singleMaxQty)){
					entity.setSingleMaxQty(new Integer(singleMaxQty));
				}	
			}
			
			if(!StringUtils.isEmpty(ransomExecCd)){
				entity.setRansomExecCd(ransomExecCd);
			}

			//垫款类型
			if(!StringUtils.isEmpty(dkStyleCd)){
				entity.setDkStyleCd(dkStyleCd);
			}
			//是否资管业务
			if(!StringUtils.isEmpty(isAssBiz)){
				entity.setIsAssBiz(isAssBiz);
				//第三方机构
				if(!StringUtils.isEmpty(thdOrgCd)){
					entity.setThdOrgCd(thdOrgCd);
				}
                //第三方机构id
                if(!StringUtils.isEmpty(thdOrgId)){
                    entity.setThdOrgId(new Long(thdOrgId));
				}
			}


			if(!StringUtils.isEmpty(startOvedayDays)){
				entity.setStartOvedayDays(new Long(startOvedayDays));
			}
			if(!StringUtils.isEmpty(endOvedayDays)){
				entity.setEndOvedayDays(new Long(endOvedayDays));
			}
			if(StringUtils.isNotEmpty(lsDtStart)){
				entity.setLsDtStart(lsDtStart);
			}
			if(StringUtils.isNotEmpty(lsDtEnd)){
				entity.setLsDtEnd(lsDtEnd);
			}
			
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			entity.setMdfUsrNm(CurrentThreadContext.getCurrentUserName());
			boolean success = this.dkRuleInfoService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败!");
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改对象失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "modify", "修改对象失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}

	/**
	 * 作废删除 
	 */
	@RequestMapping(value = "/updateIsRuleStsCd.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object updateIsRuleStsCd(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "作废 .请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("数据不存在!");
			BaseLogUtils.info(logger, "modify", "作废,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		DkRuleInfoVO ruleInfoVO = this.dkRuleInfoService.selectById(Long.valueOf(id));
		if(ruleInfoVO == null ){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("该垫款规则不存在!");
			return result;
		}
		
		if(DkRuleInfoConstant.DK_RULE_BILLSTSCD_1.equals(ruleInfoVO.getBillStsCd())){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("该垫款规则存在正在执行垫款单，请完成后再行作废!");
			return result;
		}
		
		DkRuleInfoVO entity = null;
		try {
			entity = new DkRuleInfoVO();
			entity.setId(Long.valueOf(id));
			entity.setRuleStsCd(DkRuleInfoConstant.DK_RULE_RULESTSCD_0);
			boolean success = this.dkRuleInfoService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败!");
				BaseLogUtils.info(logger, "modify", "处理失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "处理成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "处理对象失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "modify", "处理对象失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "处理对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}
	
	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "remove", "移除对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			return result;
		}
		try {
			this.dkRuleInfoService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "remove", "移除对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "remove", "移除对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "remove", "移除对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}

	/**
	 * 得到一条记录
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			//查询第三方机构
			List<FriBaseInfoVO> list = this.overdueService.getFriBaseInfoList();

			DkRuleInfoVO vo = this.dkRuleInfoService.selectById(Long.valueOf(id));

			vo.setFriBaseList(list);

			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在!");
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}
	
	/**
	 * 初始化机构银行列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initJGBankkList.do", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object initJGBankkList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String fundRasInsId = request.getParameter("fundRasInsId");
		
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		
		BankAccInfoQueryCondition qc = new BankAccInfoQueryCondition();
		qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
		qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		if(StringUtils.isEmpty(fundRasInsId)){
			return null;
		}
		qc.setFundRasInsId(new Long(fundRasInsId));
		page.setCondition(qc);
		BaseLogUtils.info(logger, "initBkList", "初始化银行列表页面数据.DATA:"+JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		
		Pager pager = this.bankAccInfoApiService.searchListPage(page);
		List<BankAccInfoDTO> list = (List<BankAccInfoDTO>) pager.getResultList();
		ListPageVO<BankAccInfoDTO> listPage = new ListPageVO<BankAccInfoDTO>(list, pager.getTotalCount());
		return listPage;
	}
	
	
	/**
	 * 到ruleInfo 详情 列表页面
	 */
	@RequestMapping(value = "/toRuleInfoDetail.do", method = { RequestMethod.GET})
	public ModelAndView toRuleInfoDetail(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.error(logger, "toListPage", "加载AddProjectInfo列表页面.");
		ModelAndView view = new ModelAndView("dk/dkRuleInfo/dkRuleInfoDetail");
		//查询第三方机构
		List<FriBaseInfoVO> list = this.overdueService.getFriBaseInfoList();
		view.addObject("friBaseList", list);

		String id = request.getParameter("id");
		view.addObject("id",id);
		return view;
	}
	
	@RequestMapping(value = "/getPrjById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getPrjById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			FncPrjBscInfoVO vo=this.fncPrjBscInfoService.selectById(Long.parseLong(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在!");
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return result;
	}
	
}

